<template>
  <base-module
    class="tower-crane-comp"
    :title="title"
    v-bind="$attrs"
    v-on="$listeners"
  >
    <swiper class="swiper-list" ref="mySwiper" :options="swiperOptions">
      <swiper-slide
        v-for="(item, index) in towerCraneData"
        :key="index"
        class="swiper-item tower-crane-container">
        <template v-if="item.children && item.children.length">
          <div class="tower-icon" :title="item.name">
            <i class="iconfont icontadiao"></i>
            <span class="status">{{ item.status }}</span>
          </div>
          <div class="tower-crane-list">
            <span
              class="tower-crane-item"
              v-for="(child, index) in item.children" :key="`child-${index}`">
              <span>{{ child.name }}</span>
              <span>{{ child.value }}</span>
            </span>
          </div>
        </template>
      </swiper-slide>
      <div  v-show="towerCraneData.length > 1" class="swiper-pagination" slot="pagination"></div>
    </swiper>
  </base-module>
</template>

<script>
import BaseModule from '@/components/public/BaseModule';
import { Swiper, SwiperSlide } from 'vue-awesome-swiper';
import 'swiper/swiper-bundle.css';

// 人员情况
export default {
  name: 'towerCraneComp',
  props: {
    title: {
      type: [String, Object],
      default: '塔吊监测'
    }
  },
  data() {
    return {
      towerCraneData: [],
      swiperOptions: {
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        loop: true,
        autoplay: true,
        speed: 1000,
        ...this.options
      }
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.$swiper;
    }
  },
  watch: {
    towerCraneData(list) {
      const hasLoop = list.length > 1;
      this.swiperOptions.autoplay = hasLoop;
      this.swiperOptions.loop = hasLoop;
    }
  },
  async created() {
    await this.getCraneList();
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.swiper.el.onmouseover = () => {
        this.swiper.autoplay.stop();
      };
      // 鼠标离开开始自动切换
      this.swiper.el.onmouseout = () => {
        this.swiper.autoplay.start();
      };
    },
    async getCraneList() {
      try {
        const { success, data } = await this.$Model.getCraneDeviceList();
        if (success && data.data) {
          const result = data.data;
          if (Array.isArray(result.data)) {
            const requestList = result.data.map(item => {
              return this.getCraneDetail(item);
            });
            if (requestList.length) {
              let towerCraneData = await Promise.all(requestList);
              towerCraneData = towerCraneData.map(item => item[0]);
              this.towerCraneData = this.setCraneData(towerCraneData);
            }
          }
        }
      } catch (e) {}
    },
    async getCraneDetail(options) {
      if (!options.id) return [];
      let resp = [];
      try {
        const { success, data } = await this.$Model.getCraneRecordList({
          crane_id: String(options.id),
          day: this.$moment().format('yyyy-MM-DD'),
          page: '1'
        });
        if (success && data.data) {
          resp = data.data.data || [];
        }
      } catch (e) {}
      return resp;
    },
    setCraneData(data) {
      if (!Array.isArray(data)) return [];
      const typeMap = {
        rotation: '回转角',
        margin: '幅度',
        load: '吊重',
        height: '高度',
        inclination: '倾角',
        torque: '力矩',
      };
      return data.map(item => {
        return {
          status: '正常运行',
          name: '',
          children: Object.keys(typeMap).map(key => ({
            ...item,
            name: typeMap[key],
            value: item[key]
          }))
        };
      });
    }
  },
  components: {
    Swiper,
    SwiperSlide,
    BaseModule
  }
};
</script>

<style lang="scss" scoped>
.tower-crane-comp {
  .tower-crane-container {
    height: 100%;
    padding-top: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    .tower-icon {
      width: 120px;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: flex-end;
      color: #fff;
      padding: 0 10px;
      cursor: pointer;
      .iconfont {
        font-size: 63px;
      }
      .status {
        min-width: 70px;
        max-width: 100px;
        height: 20px;
        background-color: #25ae89;
        border-radius: 10px;
        font-size: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 5px
      }
    }
    .tower-crane-list {
      width: 320px;
      display: flex;
      justify-content: center;
      flex-wrap: wrap;
      .tower-crane-item {
        color: #fff;
        font-size: 14px;
        margin: 5px 5px;
        width: 130px;
        height: 40px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background: rgba(141,149,195,0.4);
        border-radius: 5px;
        padding: 0 15px;
      }
    }
  }
  /deep/ .swiper-list {
    height: 100%;
    width: 100%;
    /deep/ .swiper-pagination {
      bottom: 0;
      .swiper-pagination-bullet-active {
        background: #fff;
        width: 15px;
        border-radius: 10px;
      }
    }
  }
}
</style>
